﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Greg.XmlEditor.Presentation.Presenters;

namespace Greg.XmlEditor.Presentation.Views
{
	public class DocumentNodeView : TreeNode
	{
		private readonly DocumentPresenter presenter;
		private readonly TreeNode schema;
		private readonly TreeNode style;

		public DocumentNodeView(DocumentPresenter document)
		{
			this.presenter = document;
			this.ImageKey = "document";
			this.SelectedImageKey = "document";

			this.Text = Format(this.presenter.Name, this.presenter.IsDirty, "[unknown]");
			this.Tag = document;
			
			this.schema = this.Nodes.Add(document.Schema.Name, document.Schema.Name, "schema2", "schema2");
			this.schema.Text = Format(this.presenter.Schema.Name, this.presenter.Schema.IsDirty, "[undefined]");
			schema.Tag = document.Schema;

			this.style = this.Nodes.Add(document.Schema.Name, document.Schema.Name, "style2", "style2");
			this.style.Text = Format(this.presenter.Style.Name, this.presenter.Style.IsDirty, "[undefined]");
			style.Tag = document.Style;

			this.presenter.PropertyChanged += OnDocumentPropertyChanged;
			this.presenter.Schema.PropertyChanged += OnSchemaPropertyChanged;
			this.presenter.Style.PropertyChanged += OnStylePropertyChanged;
		}

		public DocumentPresenter Presenter
		{
			get { return this.presenter; }
		}

		private void OnDocumentPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName != "Name" && e.PropertyName != "IsDirty") return;
			
			if (this.presenter.IsDirty)
			{
				this.NodeFont = new Font("Tahoma" , 8, FontStyle.Bold);
				this.ForeColor = Color.FromArgb(200, 0, 0);
			}
			else
			{
				this.NodeFont = new Font("Tahoma", 8, FontStyle.Regular);
				this.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
			}
			
			this.Text = Format(this.presenter.Name, this.presenter.IsDirty, "[unknown]");
		}

		private void OnSchemaPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "Name" || e.PropertyName == "IsDirty")
			{
				this.schema.Text = Format(this.presenter.Schema.Name, this.presenter.Schema.IsDirty, "[undefined]");
			}
		}

		private void OnStylePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "Name" || e.PropertyName == "IsDirty")
			{
				this.style.Text = Format(this.presenter.Style.Name, this.presenter.Style.IsDirty, "[undefined]");
			}
		}

		private static string Format(string name, bool isDirty, string defaultName)
		{
			return (string.IsNullOrEmpty(name) ? defaultName : name) + (isDirty ? " *" : string.Empty);
		}

		public void DetachPresenter()
		{
			this.presenter.PropertyChanged -= OnDocumentPropertyChanged;
			this.presenter.Schema.PropertyChanged -= OnSchemaPropertyChanged;
			this.presenter.Style.PropertyChanged -= OnStylePropertyChanged;
			this.Text = string.Empty;
			this.Nodes.Clear();
		}
	}
}
